home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1CAFYEF (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  8.2 KB  |  187 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.AbstractButton;
  4. import com.sun.java.swing.ButtonModel;
  5. import com.sun.java.swing.Icon;
  6. import com.sun.java.swing.JComponent;
  7. import com.sun.java.swing.LookAndFeel;
  8. import com.sun.java.swing.SwingUtilities;
  9. import com.sun.java.swing.UIManager;
  10. import com.sun.java.swing.border.Border;
  11. import com.sun.java.swing.plaf.ComponentUI;
  12. import com.sun.java.swing.plaf.ToggleButtonUI;
  13. import java.awt.Color;
  14. import java.awt.Component;
  15. import java.awt.Dimension;
  16. import java.awt.Font;
  17. import java.awt.FontMetrics;
  18. import java.awt.Graphics;
  19. import java.awt.Insets;
  20. import java.awt.Rectangle;
  21. import java.io.Serializable;
  22.  
  23. public class BasicToggleButtonUI extends ToggleButtonUI implements Serializable {
  24.    private static ToggleButtonUI toggleButtonUI;
  25.    private static BasicButtonListener listener;
  26.    private static final int defaultTextIconGap = 4;
  27.    private static final Insets defaultMargin = new Insets(2, 2, 2, 2);
  28.    protected static Color toggleButtonFocus;
  29.  
  30.    protected BasicButtonListener createListener(JComponent c) {
  31.       return new BasicButtonListener((AbstractButton)c);
  32.    }
  33.  
  34.    public static ComponentUI createUI(JComponent b) {
  35.       if (toggleButtonUI == null) {
  36.          toggleButtonUI = new BasicToggleButtonUI();
  37.       }
  38.  
  39.       return toggleButtonUI;
  40.    }
  41.  
  42.    public Border getDefaultBorder(AbstractButton b) {
  43.       return UIManager.getBorder("ToggleButton.border");
  44.    }
  45.  
  46.    public Insets getDefaultMargin(AbstractButton b) {
  47.       return defaultMargin;
  48.    }
  49.  
  50.    public int getDefaultTextIconGap(AbstractButton b) {
  51.       return 4;
  52.    }
  53.  
  54.    public Dimension getPreferredSize(JComponent c) {
  55.       AbstractButton b = (AbstractButton)c;
  56.       return BasicGraphicsUtils.getPreferredButtonSize(b, 4);
  57.    }
  58.  
  59.    protected void installDefaults(JComponent c) {
  60.       LookAndFeel.installColorsAndFont(c, "ToggleButton.background", "ToggleButton.foreground", "ToggleButton.font");
  61.       LookAndFeel.installBorder(c, "ToggleButton.border");
  62.       toggleButtonFocus = UIManager.getColor("ToggleButton.focus");
  63.    }
  64.  
  65.    protected void installKeyboardActions(JComponent c) {
  66.       listener.setupKeyboard((AbstractButton)c);
  67.    }
  68.  
  69.    protected void installListeners(JComponent c) {
  70.       if ((listener = this.createListener(c)) != null) {
  71.          ((Component)c).addMouseListener(listener);
  72.          ((Component)c).addMouseMotionListener(listener);
  73.          ((Component)c).addFocusListener(listener);
  74.          ((AbstractButton)c).addChangeListener(listener);
  75.       }
  76.  
  77.    }
  78.  
  79.    public void installUI(JComponent c) {
  80.       this.installDefaults(c);
  81.       this.installListeners(c);
  82.       this.installKeyboardActions(c);
  83.    }
  84.  
  85.    public void paint(Graphics g, JComponent c) {
  86.       AbstractButton b = (AbstractButton)c;
  87.       ButtonModel model = b.getModel();
  88.       Dimension size = ((Component)b).getSize();
  89.       FontMetrics fm = g.getFontMetrics();
  90.       Insets i = c.getInsets();
  91.       Rectangle viewRect = new Rectangle(size);
  92.       viewRect.x += i.left;
  93.       viewRect.y += i.top;
  94.       viewRect.width -= i.right + viewRect.x;
  95.       viewRect.height -= i.bottom + viewRect.y;
  96.       Rectangle iconRect = new Rectangle();
  97.       Rectangle textRect = new Rectangle();
  98.       Font f = ((Component)c).getFont();
  99.       g.setFont(f);
  100.       String text = SwingUtilities.layoutCompoundLabel(fm, b.getText(), b.getIcon(), b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, b.getText() == null ? 0 : 4);
  101.       g.setColor(((Component)b).getBackground());
  102.       if (model.isArmed() && model.isPressed() || model.isSelected()) {
  103.          this.paintButtonPressed(g, b);
  104.       }
  105.  
  106.       if (b.getIcon() != null) {
  107.          this.paintIcon(g, c, iconRect);
  108.       }
  109.  
  110.       if (text != null && !text.equals("")) {
  111.          this.paintText(g, c, textRect, text);
  112.       }
  113.  
  114.       if (b.isFocusPainted() && ((JComponent)b).hasFocus()) {
  115.          this.paintFocus(g, b, viewRect, textRect, iconRect);
  116.       }
  117.  
  118.    }
  119.  
  120.    protected void paintButtonPressed(Graphics g, AbstractButton b) {
  121.    }
  122.  
  123.    protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
  124.    }
  125.  
  126.    protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {
  127.       AbstractButton b = (AbstractButton)c;
  128.       ButtonModel model = b.getModel();
  129.       Icon icon = null;
  130.       if (!model.isEnabled()) {
  131.          icon = b.getDisabledIcon();
  132.       } else if (model.isPressed() && model.isArmed()) {
  133.          icon = b.getPressedIcon();
  134.          if (icon == null) {
  135.             icon = b.getSelectedIcon();
  136.          }
  137.       } else if (model.isSelected()) {
  138.          icon = b.getSelectedIcon();
  139.       } else if (b.isRolloverEnabled() && model.isRollover()) {
  140.          icon = b.getRolloverIcon();
  141.       }
  142.  
  143.       if (icon == null) {
  144.          icon = b.getIcon();
  145.       }
  146.  
  147.       icon.paintIcon(c, g, iconRect.x, iconRect.y);
  148.    }
  149.  
  150.    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
  151.       AbstractButton b = (AbstractButton)c;
  152.       ButtonModel model = b.getModel();
  153.       FontMetrics fm = g.getFontMetrics();
  154.       if (model.isEnabled()) {
  155.          g.setColor(((Component)b).getForeground());
  156.          BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  157.       } else {
  158.          g.setColor(((Component)b).getBackground().brighter());
  159.          BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
  160.          g.setColor(((Component)b).getBackground().darker());
  161.          BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x - 1, textRect.y + fm.getAscent() - 1);
  162.       }
  163.  
  164.    }
  165.  
  166.    protected void uninstallDefaults(JComponent c) {
  167.       LookAndFeel.uninstallBorder(c);
  168.    }
  169.  
  170.    protected void uninstallKeyboardActions(JComponent c) {
  171.       c.resetKeyboardActions();
  172.    }
  173.  
  174.    protected void uninstallListeners(JComponent c) {
  175.       ((Component)c).removeMouseListener(listener);
  176.       ((Component)c).removeMouseMotionListener(listener);
  177.       ((Component)c).removeFocusListener(listener);
  178.       ((AbstractButton)c).removeChangeListener(listener);
  179.    }
  180.  
  181.    public void uninstallUI(JComponent c) {
  182.       this.uninstallKeyboardActions(c);
  183.       this.uninstallListeners(c);
  184.       this.uninstallDefaults(c);
  185.    }
  186. }
  187.